import {getAccount, postAccount, putAccount} from '@/base/api/AccountApi';
import {Account} from '@/base/model/Account';
import {getBanners, postBanner,putbanner} from "@/base/api/BannersApi";
import {Page} from "@/base/Page";
import {Company} from "@/base/model/Company";
import {getAgentList} from "@/base/api/AgentApi";
import {deepAssign} from "@/base/util/Objects";
import {getCompanyList} from '@/base/api/CompanyApi';
import {Agent} from "@/base/model/Agent";


export class FormBloc {
  model: any = {
    bannerId:0,
    companyId: 0,
    agentId: 1,
    bannername: '',
    imageurl:'',
    status: 1,
    position:2,
    linkurl:'',
    sort: 100,
  };
  loading = false;
  fileList: Array<any> = []
  num: number = 0;
  sortList: any = [];
  companyList: any = [];
  // sortId: number = 0;
  // modelId: number = 0;
  isDetail = false;
  type: string | undefined;
  company: Page<Company> = new Page<Company>([], 0, 1, 10, 0, []);

  load(bannerId: number) {
    return getBanners(bannerId)
      .then((res) => {
        this.model.companyId = res.data.companyId;
        // this.model.agentId = res.data.agentId;
        this.model.bannername = res.data.bannername;
        this.model.imageurl = res.data.imageurl;
        this.model.status = res.data.status;
        // this.model.linkurl = res.data.linkurl;
        return res;
      }).finally(() => {
        this.hasFormInfo = true
      })
  }
  // getAgentList(pageIndex: number, pageSize: number, sortDirection: string) {
  //   this.loading = true;
  //   let that = this;
  //   return getAgentList({
  //     pageIndex,
  //     pageSize,
  //     sortDirection,
  //     companyId:this.model.companyId
  //   }).then((res) => {
  //     this.sortList = res.data.items.map(function (item) {
  //       var obj = {id: 0, agentName: ''};
  //       // @ts-ignore
  //       obj.id = item.id;
  //       obj.agentName = item.agentName;
  //       return obj;
  //     });
  //     return this.sortList;
  //   }).finally(() => {
  //     this.loading = false;
  //   });
  // }

  getCompanyList(pageIndex: number,
                 pageSize: number,
                 sortDirection: string) {
    return getCompanyList({pageIndex, pageSize, sortDirection, name: '', createBegin: ''})
      .then((res) => {
        deepAssign(this.company, res.data)
        this.companyList = res.data.items.map(item => {
          let obj = {companyId: 0, companyName: ''}
          obj.companyId = item.id;
          obj.companyName = item.name
          return obj
        })
        return res;
      }).finally(() => {
        this.loading = false;
      });
  }

  hasFormInfo = false;



  save(model:any) {
    this.loading = true;
    return putbanner({
      bannerId:model.bannerId,
      agentId: model.agentId,
      bannername: model.bannername,
      companyId: model.companyId,
      imageurl: model.imageurl,
      linkurl:model.linkurl,
      position:model.position,
      status:model.status,
      sort: model.sort
    }).then((res) => {
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }
}
